#include "stdafx.h"
#include "aviprocessor.h"

unsigned __stdcall processImages( void *g) {

	AviProcessor *proc= reinterpret_cast<AviProcessor *>(g);

	while (!proc->isStopped()) {

		if(cvGrabFrame(proc->capture)) {

			proc->img=cvRetrieveFrame(proc->capture);           

			// calling the process function
			if (proc->callingProcess())
				proc->process(proc->img);
			else
				Sleep(proc->getDelay());

			// displays image and frame number
			if (proc->isDisplayed()) {

				CvScalar sc;
				char text[50];
				sprintf(text,"Frame #%6d",proc->getFrameNumber());

				sc= cvGet2D(proc->img,20,20);
				if (sc.val[1]>128) 
					cvPutText(proc->img, text, cvPoint(20,20), &(proc->font), cvScalar(0,0,0));
				else
					cvPutText(proc->img, text, cvPoint(20,20), &(proc->font), cvScalar(255,255,255));


				cvShowImage( "Image (AviProcessor)", proc->img );
				HWND hWnd= (HWND)cvGetWindowHandle("Image (AviProcessor)");
				::SendMessage(hWnd,WM_PAINT,NULL,NULL); // force the window to repaint
			}

			// increment frame number
			proc->incFrameNumber();

		} else {

			proc->stopIt();
		}
	}

	_endthreadex( 0 );
	return 0;
}
